package org.cx.entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.cx.util.LogInfoUtil;


public class Page {
	private int showCount = 10;   //当前返回条数
	private int totalPage;   //总页数
	private int totalResult;   //总条数据
	private int currentPage;  //当前页数
	private int currentResult = 0;  //获取数据起点（从第几条开始）
	private Object pd ;
	
	HttpServletRequest request;
	
	public Page(){
		
	}
	
	public Page(HttpServletRequest request) {
		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();

		String name = "";
		String value = "";

		while (entries.hasNext()) {
			Map.Entry entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (valueObj == null) {
				value = "";
			} else if ((valueObj instanceof String[])) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}

			LogInfoUtil.info(this.getClass(), "参数  ", name + " == " + value);
			
			if("currentPage".equals(name)){  //当前页
				this.currentPage = Integer.parseInt(value);
			}else if("showCount".equals(name)){  //当前返回条数
				this.showCount = Integer.parseInt(value);
			}else{
				returnMap.put(name, value);
			}
		}
		this.pd = new PageData(request);
	}
	

	public int getTotalPage() {
		if (this.totalResult % this.showCount == 0)
			this.totalPage = (this.totalResult / this.showCount);
		else
			this.totalPage = (this.totalResult / this.showCount + 1);
		return this.totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalResult() {
		return this.totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public int getCurrentPage() {
		if (this.currentPage <= 0)
			this.currentPage = 1;
		if (this.currentPage > getTotalPage())
			this.currentPage = getTotalPage();
		return this.currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getShowCount() {
		return this.showCount;
	}

	public void setShowCount(int showCount) {
		this.showCount = showCount;
	}

	public int getCurrentResult() {
		this.currentResult = ((getCurrentPage() - 1) * getShowCount());
		if (this.currentResult < 0)
			this.currentResult = 0;
		return this.currentResult;
	}

	public void setCurrentResult(int currentResult) {
		this.currentResult = currentResult;
	}

	public Object getPd() {
		return this.pd;
	}

	public void setPd(Object pd) {
		this.pd = pd;
	}

	private class pageExt {
		private pageExt() {
			getClass();
		}
	}
}
